package njust.user;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;

public class LoginController extends HttpServlet {
    public LoginController(){
        super();
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    protected String getCollegeName(int c){
        switch (c){
            case 1:return "计算机学院";
            case 2:return "理学院";
            case 3:return "外国语学院";
        }
        return null;
    }
    protected String getSubjectName(int c,int s){
        if(c==1)
        {
            switch (s){
                case 1:return "计算机科学与技术";
                case 2:return "软件工程";
                case 3:return "人工智能";
            }
        }
        else if(c==2)
        {
            switch (s){
                case 1:return "数学系";
                case 2:return "物理学系";
                case 3:return "天文学系";
                case 4:return "化学系";
            }
        }
        else if(c==3)
        {
            switch (s){
                case 1:return "英语系";
                case 2:return "日语系";
                case 3:return "法语系";
            }
        }
        return null;
    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //表单已成功提交到此处
        HttpSession httpSession= req.getSession();
        //验证码可能在之后需要httpSession

        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String checkcode = req.getParameter("checkcode");


        if (checkcode!=null&&checkcode.equals("7364")){
            if (req.getParameter("college")!=""&&req.getParameter("subject")!=""){
                int college=Integer.parseInt(req.getParameter("college"));
                int subject = Integer.parseInt(req.getParameter("subject"));
                Login login=new Login(username,password,getCollegeName(college),getSubjectName(college,subject));
                LoginService loginService=new LoginService();
                int result=loginService.validateLoginInfo(login);
                if(result == 1){
                    httpSession.setAttribute("userinfo",login);
                    req.getRequestDispatcher("main.jsp").forward(req,resp);
                }else{
                    req.setAttribute("error_code", result);
                    req.getRequestDispatcher("loginFailure.jsp").forward(req,resp);
                }
            }
            else {
                req.setAttribute("error_code",7);
                req.getRequestDispatcher("loginFailure.jsp").forward(req,resp);
            }

        }
        else {
            req.setAttribute("error_code", 4);  //验证码错误
            req.getRequestDispatcher("loginFailure.jsp").forward(req,resp);
        }
    }
}
